package com.nkzy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nkzy.domain.dto.EmpFormDTO;
import com.nkzy.domain.pojo.Result;
import com.nkzy.domain.pojo.Emp;
import com.nkzy.domain.vo.EmpVO;
import com.nkzy.service.EmpService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emps")  // RESTful风格路径
@RestController
@CrossOrigin
public class EmpController {

    @Autowired
    private EmpService empService;

    // 新增员工
    @PostMapping
    @Operation(summary = "保存员工信息")
    public Result saveEmp(@RequestBody EmpFormDTO empFormDTO) {
        Emp emp = BeanUtil.copyProperties(empFormDTO, Emp.class);
        empService.save(emp);
        return Result.success();
    }

    // 查询单个员工
    @GetMapping("/{id}")
    @Operation(summary = "查询一个员工")
    public Result getEmp(@PathVariable Long id) {
        Emp emp = empService.getById(id);
        EmpVO empVO = BeanUtil.copyProperties(emp, EmpVO.class);
        return Result.success(empVO);
    }

    // 删除员工
    @DeleteMapping("/{id}")
    @Operation(summary = "删除一个员工")
    public Result deleteEmp(@PathVariable Long id) {
        empService.removeById(id);
        return Result.success();
    }

    // 批量查询员工
    @GetMapping
    @Operation(summary = "根据id的集合查询多个员工")
    public Result getEmps(@RequestParam("ids") List<Long> ids) {
        List<Emp> emps = empService.listByIds(ids);
        List<EmpVO> empsVO = BeanUtil.copyToList(emps, EmpVO.class);
        return Result.success(empsVO);
    }

    // 查询所有员工
    @GetMapping("/all")
    @Operation(summary = "查询所有员工")
    public Result getAllEmps() {
        List<Emp> emps = empService.list();
        List<EmpVO> empsVO = BeanUtil.copyToList(emps, EmpVO.class);
        return Result.success(empsVO);
    }

    // 修改员工信息
    @PutMapping
    @Operation(summary = "修改一个员工")
    public Result updateEmp(@RequestBody EmpFormDTO empFormDTO) {
        Emp emp = BeanUtil.copyProperties(empFormDTO, Emp.class);
        empService.updateById(emp);
        return Result.success();
    }
}